package com.hntest.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hntest.dao.IpLogDao;
import com.hntest.entity.IpLog;
import com.hntest.entity.LoginInfor;
import com.hntest.entity.Page;
import com.hntest.service.IpLogService;

/**
 * 登陆流水业务实现类
 * @author ZHOU
 *
 */
@Service("ipLogService")
public class IpLogServiceImpl implements IpLogService{
	@Autowired
	private IpLogDao ipLogDao;

	public void setIpLogDao(IpLogDao ipLogDao) {
		this.ipLogDao = ipLogDao;
	}

	//登记登陆流水
	@Override
	public int writLoginFlow(LoginInfor user) throws UnknownHostException {
		InetAddress ip = null;
		ip = ip.getLocalHost();
		IpLog ipLog = new IpLog();
		ipLog.setIdIp(ip.getHostAddress());
		ipLog.setUserInfo(user);
		ipLog.setLoginTime(new Date());
		int n = ipLogDao.writLoginFlow(ipLog);
		return n;
	}

	//计算总流水记录数
	@Override
	public int getCount() {
		int n = ipLogDao.getCount();
		return n;
	}

	//分页查询登陆流水
	@Override
	public List<IpLog> getListByPage(Page page, LoginInfor user) {
		IpLog ipLog = new IpLog();
		ipLog.setUserInfo(user);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("pageStart",((page.getCurrPage()-1)*page.getPageSize()+1));
        m.put("pageEnd",(page.getCurrPage()*page.getPageSize()));
        m.put("condition", ipLog);
        return ipLogDao.getListByPage(m);
	}

	//根据条件查询登陆流水记录的总数
	@Override
	public int getConditionCount(LoginInfor user) {
		return ipLogDao.getConditionCount(user);
	}
	
}
